import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/model/store_category_model.dart';
import 'package:flutter_douban/main_page/store/provider/store_count_model.dart';
import 'package:flutter_douban/tool_views/cached_network_Image.dart';
import 'package:provider/provider.dart';

class ShopingCell extends StatefulWidget {
  final StoreCategoryModel model;
  const ShopingCell({Key? key, required this.model}) : super(key: key);

  @override
  _ShopingCellState createState() => _ShopingCellState();
}

class _ShopingCellState extends State<ShopingCell> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 10),
      height: 155,
      width: MediaQuery.of(context).size.width,
      color: Colors.white,
      child: Column(
        children: [
          /// 商品view
          Container(
            width: MediaQuery.of(context).size.width,
            height: 110,
            color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(
                  width: 50,
                  height: 50,
                  child: TextButton(
                    onPressed: () {
                      setState(() {
                        if (widget.model.is_select == null ||
                            widget.model.is_select == false) {
                          widget.model.is_select = true;
                        } else {
                          widget.model.is_select = false;
                        }

                        /// 更新结算总价格
                        Provider.of<StoreCountModel>(context, listen: false)
                            .pay(widget.model);
                      });
                    },
                    child: Image.asset(
                      widget.model.is_select == true
                          ? 'assets/images/common/login_check_button_select@2x.png'
                          : 'assets/images/common/login_check_button_normal@2x.png',
                      width: 20,
                      height: 20,
                    ),
                  ),
                ),
                _rowWidget()
              ],
            ),
          ),

          /// 分割线
          Container(
            width: MediaQuery.of(context).size.width,
            height: 1,
            color: AppColors.page,
          ),

          /// 合计价钱
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.only(left: 10, right: 10),
            width: MediaQuery.of(context).size.width,
            height: 35,
            color: Colors.white,
            child: RichText(
              text: TextSpan(
                text: '小计: (共${widget.model.r.toString()}件) :',
                style: TextStyle(
                  fontSize: 12,
                  color: AppColors.qianTextColor,
                ),
                children: [
                  TextSpan(
                    text:
                        '   ¥${(NumUtil.getNumByValueDouble(double.parse(widget.model.p!) * widget.model.r!, 2))!.toStringAsFixed(2)}',
                    style: TextStyle(
                        fontSize: 15,
                        color: Colors.redAccent,
                        fontWeight: FontWeight.w400),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _rowWidget() {
    return Container(
      width: MediaQuery.of(context).size.width - 50,
      color: Colors.white,
      padding: EdgeInsets.only(right: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          ClipRRect(
            child: CachedImage(
                url: widget.model.ti?.isNotEmpty == true
                    ? widget.model.ti!
                    : widget.model.i!,
                width: 80,
                height: 80),
            borderRadius: BorderRadius.circular(8),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 15,
              ),
              Row(
                children: [
                  Container(
                    width: MediaQuery.of(context).size.width - 210,
                    height: 20,
                    child: Text(
                      widget.model.t?.isNotEmpty == true
                          ? widget.model.t!
                          : widget.model.n!,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 14,
                        color: AppColors.black,
                      ),
                    ),
                  ),
                  Text(
                    '¥${widget.model.p}',
                    style: TextStyle(
                        fontSize: 16,
                        color: Colors.redAccent,
                        fontWeight: FontWeight.w400),
                  )
                ],
              ),
              SizedBox(
                height: 30,
              ),
              Container(
                width: 90,
                height: 30,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(6),
                  border: Border.all(width: 1, color: AppColors.line),
                ),
                child: Row(
                  children: [
                    InkWell(
                      child: Container(
                        alignment: Alignment.center,
                        width: 29,
                        height: 30,
                        color: AppColors.page,
                        child: Text(
                          '-',
                          style: TextStyle(
                              fontSize: 22,
                              color: Colors.black45,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                      onTap: () {
                        Provider.of<StoreCountModel>(context, listen: false)
                            .remove(widget.model);
                      },
                    ),
                    Container(
                      alignment: Alignment.center,
                      width: 29,
                      height: 30,
                      child: Text(
                        widget.model.r.toString(),
                        style: TextStyle(
                          fontSize: 14,
                          color: Colors.black,
                        ),
                      ),
                    ),
                    InkWell(
                      child: Container(
                        alignment: Alignment.center,
                        width: 30,
                        height: 30,
                        color: AppColors.page,
                        child: Text(
                          '+',
                          style: TextStyle(
                              fontSize: 22,
                              color: Colors.black45,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                      onTap: () {
                        Provider.of<StoreCountModel>(context, listen: false)
                            .add(widget.model);
                      },
                    )
                  ],
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
